package com.qinghe.utils;

import java.io.Serializable;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

/**
 * form提交后返回json数据结构
 * 
 * <form method="post" action="" onsubmit="return beforeSubmit(this, callback);">
 * statusCode=statusCode.ok表示操作成功, 做页面跳转等操作.
 * statusCode=statusCode.error表示操作失败, 提示错误原因.
 * statusCode=statusCode.timeout表示session超时
 * {"statusCode":"200", "message":"操作成功"}
 * {"statusCode":"300", "message":"操作失败"}
 * {"statusCode":"301", "message":"会话超时"}
 * {"statusCode":"403", "message":"权限不足"}
 * 
 * @author longfei
 */
@JsonAutoDetect
@JsonIgnoreProperties(value = {"forwardUrl"})
public class AjaxObject implements Serializable {

	private static final long serialVersionUID = -6260394377996144893L;
	/**
	 * 状态码
	 */
	public final static int STATUS_CODE_SUCCESS = 200;
	public final static int STATUS_CODE_FAILURE = 300;
	public final static int STATUS_CODE_TIMEOUT = 301;
	public final static int STATUS_CODE_FORBIDDEN = 403;

	private int statusCode = STATUS_CODE_SUCCESS;
	private String message = "";
	private String forwardUrl = "";
	private Object data;

	public AjaxObject() {

	}

	public AjaxObject(String message) {
		this.message = message;
	}

	public AjaxObject(int statusCode) {
		super();
		this.statusCode = statusCode;
	}

	/**  
	 * 构造函数
	 * @param statusCode
	 * @param message  
	 */ 
	public AjaxObject(int statusCode, String message) {
		super();
		this.statusCode = statusCode;
		this.message = message;
	}

	public int getStatusCode() {
		return statusCode;
	}

	public void setStatusCode(int statusCode) {
		this.statusCode = statusCode;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getForwardUrl() {
		return forwardUrl;
	}

	public void setForwardUrl(String forwardUrl) {
		this.forwardUrl = forwardUrl;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public static AjaxObject newOk(String message) {
		return new AjaxObject(STATUS_CODE_SUCCESS, message);
	}

	public static AjaxObject newError(String message) {
		return new AjaxObject(STATUS_CODE_FAILURE, message);
	}

	public static AjaxObject newTimeout(String message) {
		return new AjaxObject(STATUS_CODE_TIMEOUT, message);
	}	

	public static AjaxObject newForbidden(String message) {
		return new AjaxObject(STATUS_CODE_FORBIDDEN, message);
	}

	public static AjaxObject newForward(String forwardUrl, String message) {
		AjaxObject ajaxObject = new AjaxObject(STATUS_CODE_SUCCESS);
		ajaxObject.forwardUrl = forwardUrl;
		ajaxObject.message = message;
		return ajaxObject;
	}

	public static AjaxObject newData(Object data) {
		AjaxObject ajaxObject = new AjaxObject(STATUS_CODE_SUCCESS);
		ajaxObject.data = data;
		return ajaxObject;
	}

	public static AjaxObject newData(Object data, String message) {
		AjaxObject ajaxObject = new AjaxObject(STATUS_CODE_SUCCESS);
		ajaxObject.data = data;
		ajaxObject.message = message;
		return ajaxObject;
	}

	@Override
	public String toString() {
		StringBuffer buffer = new StringBuffer();
		buffer.append("{")
			.append( "\"statusCode\":\"" + statusCode + "\",")
			.append("\"message\":\"" + message + "\",")
			.append("\"forwardUrl\":\"" + forwardUrl + "\"")
			.append("}");
		return buffer.toString();
	}

}
